<?php

$addr = address();
$rel_addr = relative_address();

// OB_START
ob_start();

/*
**  S I T E   V A R I A B L E S
*/
$SITE_NAME = 'Non-Profit Exchange';

/*
**   S E S S I O N S
*/
include $rel_addr.'/sessions/login_session.php';
include $rel_addr.'/sessions/profile_session.php';
include $rel_addr.'/sessions/search_session.php';

/*
**   M Y S Q L 
*/
	function db_connect ($db_url, $db, $user, $pw){
		if($dbc = @mysql_connect($db_url,$user,$pw)){
			if (!@mysql_select_db ($db)){
				return die('<p>Could not select the database because: <B>' . mysql_error() . '');
			}else{
				return $dbc;
			}
		}else{
				return die('<P>Could not connect to the MYSQL because: <B>' . mysql_error() . '');
		}
	}
	
	function table_query ($table, $page = 'none',$sort = 'none'){
		if($page == 'none' && $sort == 'none'){
			return mysql_query("SELECT * FROM $table");
		}else if($page == 'none'){
			return mysql_query("SELECT * FROM $table ORDER BY $sort");
		}else if($sort == 'none'){
			return mysql_query("SELECT * FROM $table WHERE page='$page'");
		}else{
			return mysql_query("SELECT * FROM $table WHERE page='$page' ORDER BY $sort");
		}
	}

	function table_array ($table,$page,$sort){
		return mysql_fetch_array(table_query($table,$page,$sort));
	}

/*
**   P H P
*/
	function address (){
		return 'http://'.$_SERVER['SERVER_NAME'].'';
	}
	function relative_address(){
		return '/home/a2801869/public_html';
	}

	function is_ie (){
		//Not Internet Explorer
		if(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE') === FALSE) {
			return false;
		//Is Internet Explorer
		}else{
			return true;
		}
	}
	
	function make_unique_id($string){
		$unique_id = strlen($string);
		$y = 3;
		$string = ' '.$string;
		$size = strlen($string);
		
		for($x = 1; $x < $size; $x++){
			if($x % 2 == 0){
				$y+= (ord($string[$x])*$x);
			}else{
				$y+= (int)(ord($string[$x]) / $x) + (int)(ord($string[$x]) / 2);
			}
			$unique_id += $x*ord($string[$x]) + (10*$x) + $y;
		}
		return $unique_id;
	}

	//Retrieves variable page from URL
	function this_content (){
		return $_REQUEST['page'];
	}
	
	//Retrieves page's name i.e. index.php
	function this_page(){
		return $_SERVER['PHP_SELF'];
	}
	
	// Goto if object is clicked
	function click_get($var_name, $var){
		return 'OnClick="parent.location=\''.this_page().'?'.$var_name.'='.$var.'\'"';
	}
	
	// Refresh if item is selected
	function click_refresh($page = 'default'){
		if($page == 'default'){
			return 'OnClick="parent.location=\''.this_page().'\'"';
		}else {
			return 'OnClick="parent.location=\''.$page.'\'"';
		}
	}
	
	// Submit form on click
	function click_submit(){
		return 'OnClick="this.form.submit()"';
	}

	// Refresh a page
	function refresh ($page = 'none', $dbpage = 'none',$seconds = 0){
		$address = address();
		switch(TRUE){
			case ($page != 'none' && $dbpage != 'none'):
				return header('refresh: '.$seconds.'; url='.$address.$page.'?page='.$dbpage.'');
				break;
			case ($page != 'none'):
				return header('refresh: '.$seconds.'; url='.$address.$page.'');
				break;
			case($dbpage != 'none'):
				return header('refresh: '.$seconds.'; url='.$address.this_page().'?page='.$dbpage.'');
				break;
			case($page == 'none'):
				return header('refresh: '.$seconds.'; url='.$address.this_page().'');
				break;
		}
		echo "$address.this_page()";
	}

	function title ($title){
		if(!empty($title)){
			return '<TITLE>'.$GLOBALS['SITE_NAME'].' - '.$title.'</TITLE>';
		}else{
			return '<TITLE>'.$GLOBALS['SITE_NAME'].'</TITLE>';
		}
	}

	function style ($style){
		$reset = '/style/reset.css';
		if(empty($style)){
			$style = '/style/style.css';
			return '<link type="text/css" rel="stylesheet" href="'.address().$reset.'">
				<link type="text/css" rel="stylesheet" href="'.address().$style.'">';
		}else{
			return '<link type="text/css" rel="stylesheet" href="'.address().$reset.'">
				<link type="text/css" rel="stylesheet" href="'.address().$style.'">';
		}
	}

	function navigation ($nav){
		if(empty($nav)){
			return '
				
				';
		}else{
			return $nav;
		}
	}
	function head ($header){
		if(empty($header)){
			return ''.$GLOBALS['SITE_NAME'].'';
		}else{
			return $header;
		}
	}
	
/*
**    P A G E 
**      C R E A T I O N
*/
	function top ($title, $style){
		
		$return ='
		<html>
		<head>

		'.title($title).'

		'.style($style).'

		</head>
		<body>
		';
		
		echo $return;
	}

	function middle ($header,$subtitle,$content,$side = ''){
		$return .= '
		<div id="big_box">
			<div class="roundedcornr_box">
			<div class="roundedcornr_top"><div></div></div>
			<div id="header">
			'.$header.'
			</div>
	
			<div id="middle">
				<div id="sidenav">
				'.$side.'
				</div>
		
				<div id="center_content">
					<p class="header">'.$subtitle.'</p>
					'.$content.'
				</div>
			</div>
			';
		echo $return;
	}

	function bottom ($nav,$footer = 'All Rights Reserved',$dbc){
		$date = date("m/d/Y");

			$return = '
			<div id="footer">
			'.navigation($nav).'
			
			'.$footer.'
			</div>
			<div class="roundedcornr_bottom"><div></div></div>
			</div>
		</div>
			</body>
	
		
			</html>
			';
		
		echo $return;
		
		ob_end_flush();
		mysql_close($dbc);
	}
	
	//
	//    N O T E S
	//
	/*
	**		<?php
	**		  $string = 'Hello World!';
	**		  if(stristr($string, 'earth') === FALSE) {
	**			echo '"earth" not found in string';
	**		  }
	**		  outputs: "earth" not found in string
	**		?>
	**
	*/
?>
